class SiteConfigsController < ApplicationController
  load_and_authorize_resource

  def index
    @wechat_config = SiteConfig.wechat_month_reward
    @preview = preivew
  end

  def update
    @site_config = SiteConfig.find params[:id]
    if @site_config.update(site_config_params)
      redirect_to site_configs_url, notice: '修改成功'
    else
      redirect_to site_configs_url, alert: '修改失败'
    end
  end


  private
  def site_config_params
    params.require(:site_config).permit(:id, :key, :value)
  end

  def preivew
    current_month = Date.current
    previous_month = Date.current.prev_month


    template = SiteConfig.wechat_month_reward.value
    msg = template.gsub('#name#', '小张')
              .gsub('#previous_month#', previous_month.strftime('%Y-%m'))
              .gsub('#previous_month_reward#', '2000')
              .gsub('#current_month#', current_month.strftime('%Y-%m'))
              .gsub('#current_month_reward#', '3500')
    msg
  end
end
